package com.robot.admin.controller;

import com.robot.admin.pojo.Area;
import com.robot.admin.pojo.City;
import com.robot.admin.service.AreaService;
import com.robot.admin.service.CityService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * city控制器层
 *
 * @author lin
 */
@RestController
@CrossOrigin
@RequestMapping("/city")
public class CityController {
    @Autowired
    private CityService cityService;
    @Autowired
    private AreaService areaService;


    /**
     * 根据code查找city
     *
     * @param city
     * @return
     */
    @RequestMapping(value = "findByCode", method = RequestMethod.POST)
    public Result findByCode(@RequestBody City city) {
        return new Result(StatusCode.SUCCESS_QUERY, cityService.findByCode(city.getCode()));
    }

    /**
     * 根据name查找city
     *
     * @param city
     * @return
     */
    @RequestMapping(value = "findByName", method = RequestMethod.POST)
    public Result findByName(@RequestBody City city) {
        return new Result(StatusCode.SUCCESS_QUERY, cityService.findByName(city.getName()));
    }


    /**
     * 根据code查找area列表
     *
     * @param city
     * @return
     */
    @RequestMapping(value = "findAreasByCode/{page}/{size}", method = RequestMethod.POST)
    public Result findAreasByCode(@RequestBody City city, @PathVariable int page, @PathVariable int size) {
        Page<Area> pageData = areaService.findAllByCodeLike(city.getCode(), page, size);
        return new Result(StatusCode.SUCCESS_QUERY, new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
    }
}
